package com.ai.gcf.api.base.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import com.ai.gcf.api.base.constants.Constants.CharsetName;
/**
 * 系统通用静态方法
 * @author zhujl 2014-9-17 新建
 * <li>修改日期：修改人：修改说明：新增XX方法</li>
 * <li>修改日期：修改人：修改说明：新增XXX接口，废弃XX接口，但出于兼容性考虑仍然保留XX接口</li>
 * <li>修改日期：修改人：修改说明：修改XX接口的实现</li>
 */
public class CommonMethods {
	public static final String module = CommonMethods.class.getName();
	/**
	 * 获取当前主机IP
	 * @return
	 * 
	 */
	public static String getCurrentHostIp(){
		InetAddress host = null;
		String hostAddress="UnknownHostException";
		try {
			host = InetAddress.getLocalHost();
			hostAddress = host.getHostAddress();
			System.out.println("host address :"+hostAddress);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return hostAddress;
	}
	/**
	 * 获取当前主机名称
	 * @return
	 */
	public static String getCurrentHostName(){
		InetAddress host = null;
		String hostName="UnknownHostException";
		try {
			host = InetAddress.getLocalHost();
			hostName = host.getHostName();
			System.out.println("host address :"+hostName);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return hostName;
	}
	/**
	 * 获取当前进程ID
	 * @return
	 */
	public static long getCurrentPID() {
	    String processName =
	        java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
	    return Long.parseLong(processName.split("@")[0]);
	}
	/**
	 * 获取当前进程ID
	 * @return
	 */
	public static String getCurrentProcessName() {
		return	java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
		 
	}
	
	/**
	 * 字符串最大长度截取
	 * @param str 源字符串
	 * @param max 最大字节数
	 * @return
	 */
	public static String iftruncate(String str, int max) {
		try {
			if(max<1){
				return str;
			}
			if (str != null ) {
				byte[] buf = str.getBytes(CharsetName.GBK);
				if(buf.length > max){
					int count = 0;
					for(int x=max-1; x>=0; x--)
					{
						if(buf[x]<0)
							count++;
						else
							break;
					}//for
					//截取，截取后的字符串长度<=max字节
					if(count%2!=0){//不含中文
						return new String(buf,0,max-1,CharsetName.GBK);
					}else{//含中文
						return new String(buf,0,max,CharsetName.GBK);
					}//if-else
				}//if
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return str;
	}
	
	 /**
     * 构造本地路径
     * @param fileId
     * @return
     */
    public static String buildLocalPath(String fileId){
    	String localPath = null;
    	String tmpdir = System.getProperty("java.io.tmpdir") ;
    	if(tmpdir == null || tmpdir.equals("")){
    		//throw new BusinessException(Special.PARAM_IS_NULL, "JAVA临时目录为空");
    		tmpdir = "."+File.separator;
    	}
    	
    	localPath = tmpdir+File.separator+fileId;
    	return localPath;//取java临时目录
    }
    /**
     * 获取本地临时路径
     * @param fileId
     * @return
     */
    public static String getLocalPath(){
    	String localPath = null;
    	String tmpdir = System.getProperty("java.io.tmpdir") ;
    	if(tmpdir == null || tmpdir.equals("")){
    		//throw new BusinessException(Special.PARAM_IS_NULL, "JAVA临时目录为空");
    		tmpdir = "."+File.separator;
    	}
    	
    	localPath = tmpdir+File.separator;
    	return localPath;//取java临时目录
    }
    
	/**
	 * @param args
	 */
	public static void main(String[] args)throws Exception {
		//testTruncate();
		testTruncate2();
	}
	
	public static void testTruncate()throws Exception{
//		String str = "我abc汉字  ,6";
		String str = "我abc汉字  ,6";
		for(int i=0;i<=str.getBytes("gbk").length;++i){
			System.out.println(i+" "+iftruncate(str,i));
		}
	}
	public static void testTruncate2()throws Exception{
		File f= new File("F:\\test\\testdata.txt");
		StringBuffer sb = new StringBuffer();
		BufferedReader br = new BufferedReader(new FileReader(f));
		while(br.ready()){
			sb.append(br.readLine()).append("\n");
		}
		sb.append("张三");
		String str = sb.toString();
		System.out.println(str.getBytes("gbk").length+" "+iftruncate(str,str.getBytes("gbk").length));
	}
	
	
	

}
